package com.example.homework_exam

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.example.homework_exam.database.SQLiteHelper
import com.example.homework_exam.utils.DBUtils

class RecordActivity : Activity(), View.OnClickListener {
    var note_back: ImageView? = null
    var note_time: TextView? = null
    var content: EditText? = null
    var delete: ImageView? = null
    var note_save: ImageView? = null
    var mSQLiteHelper: SQLiteHelper? = null
    var noteName: TextView? = null
    var id: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_record)
        note_back = findViewById<View>(R.id.note_back) as ImageView
        note_time = findViewById<View>(R.id.tv_time) as TextView
        content = findViewById<View>(R.id.note_content) as EditText
        delete = findViewById<View>(R.id.delete) as ImageView
        note_save = findViewById<View>(R.id.note_save) as ImageView
        noteName = findViewById<View>(R.id.note_name) as TextView
        note_back!!.setOnClickListener(this)
        delete!!.setOnClickListener(this)
        note_save!!.setOnClickListener(this)
        initData()
    }

    protected fun initData() {
        mSQLiteHelper = SQLiteHelper(this)
        noteName!!.text = getString(R.string.add_record)
        val intent = intent
        if (intent != null) {
            id = intent.getStringExtra("id")
            if (id != null) {
                noteName!!.text = getString(R.string.edit_record)
                content!!.setText(intent.getStringExtra("content"))
                note_time!!.text = intent.getStringExtra("time")
                note_time!!.visibility = View.VISIBLE
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.note_back -> finish()
            R.id.delete -> content!!.setText(getString(R.string.empty))
            R.id.note_save -> {
                val noteContent = content!!.text.toString().trim { it <= ' ' }
                if (id != null) { //修改操作
                    if (noteContent.length > 0) {
                        if (mSQLiteHelper!!.updateData(id, noteContent, DBUtils.getTime())) {
                            showToast(getString(R.string.modify_success))
                            setResult(2)
                            finish()
                        } else {
                            showToast(getString(R.string.modify_failed))
                        }
                    } else {
                        showToast(getString(R.string.content_cannot_be_empty))
                    }
                } else {
                    //向数据库中添加数据
                    if (noteContent.length > 0) {
                        if (mSQLiteHelper!!.insertData(noteContent, DBUtils.getTime())) {
                            showToast(getString(R.string.save_success))
                            setResult(2)
                            finish()
                        } else {
                            showToast(getString(R.string.save_failed))
                        }
                    } else {
                        showToast(getString(R.string.content_cannot_be_empty))
                    }
                }
            }
        }
    }

    fun showToast(message: String?) {
        Toast.makeText(this@RecordActivity, message, Toast.LENGTH_SHORT).show()
    }
}
